﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;

namespace Core.DMS
{
    public class Document : FileSystemObject
    {
        int _size = -1;
        string _filename = "";
        string _description = "";
        string _mimeType = "application/octet-stream";
        DateTime _created;
        DateTime _lastupdate;

        public int Size { get{ return _size;} }
        public string Filename { get { return _filename; } set { _filename = value; } }
        public string Description { get { return _description; } set { _description = value;} }
        public string Path { get { return Folder.Get(Parent).Path + "/" + Title; } }
        public DateTime Created { get { return _created; } }
        public DateTime LastUpdated { get { return _lastupdate; } }

        public string MimeType { get { return _mimeType; } }

        public Document(int Parent, string Title) :base(Parent, Title)
        {

        }

        public Document(IDataReader data) : base(data)
        {
            _description = (string)data["description"];
            _created = (DateTime)data["created"];
            _lastupdate = (DateTime)data["LastUpdated"];
            _size = (int)data["size"];
            _parent = (int)data["parent"];
            _title = (string)data["title"];
            _filename = (string)data["originalfilename"];
        }

        public void Save(Stream ContentStream)
        {
            if (ContentStream == null)
                throw new Exception("When creating a new document you must specify the content before saving.");

            _id = AppFunctions.Documents.SaveDocument(this, ContentStream);
        }

        public static Document Get(int DocumentID) { return AppFunctions.Documents.GetDocument(DocumentID); }

        public void GetContent(Stream outputStream)
        {
            AppFunctions.Documents.GetDocumentContent(ID, outputStream);
        }

        public void SetContent(Stream newContentStream)
        {
            Save(newContentStream);
        }
    }
}
